import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { articleApi } from '@/api/article'

export const useSearchStore = defineStore('search', () => {
    const searchResults = ref([])
    const searchKeyword = ref('') // 保存搜索关键词，用于搜索页面显示
    const total = ref(0)
    const loading = ref(false)

    // 搜索方法
    async function searchArticles(searchParams) {
        try {
            loading.value = true
            const response = await articleApi.searchArticles(searchParams)
            searchResults.value = response.data.items
            total.value = response.data.total
            // 保存搜索关键词
            searchKeyword.value = searchParams.keyword || ''
            return response // 返回结果，让调用方知道搜索是否成功
        } finally {
            loading.value = false
        }
    }

    return {
        searchResults,
        searchKeyword,
        total,
        loading,
        searchArticles
    }

})
